;(function () {
    importCss();
    var _global;
    var Dialog = function (retId,option,active) {
        var _this=this;
        var defaults={
            yes:'确认',
            no:'取消'
        };
        this.o=extend(defaults,option);
        // console.log(this.o)
        _global.dialogYesBtn_json={
            go_top:'',
            go_bottom:'',
            go_left:'',
            go_right:'dialogNoBtn'
        };
        _global.dialogYesBtn_onfocus=function (code,id) {
            $('#'+id).addClass('active');
        };
        _global.dialogYesBtn_onblur=function (code,id) {
            $('#'+id).removeClass('active');
        };
        _global.dialogYesBtn_onsubmit=function (code,id) {
            _this.close();
            keyopt.options.focusElm=retId;
            $('#'+retId).addClass(active||'active');
        };
        _global.dialogNoBtn_json={
            go_top:'',
            go_bottom:'',
            go_left:'dialogYesBtn',
            go_right:''
        };
        _global.dialogNoBtn_onfocus=function (code,id) {
            $('#'+id).addClass('active');
        };
        _global.dialogNoBtn_onblur=function (code,id) {
            $('#'+id).removeClass('active');
        };
        _global.dialogNoBtn_onsubmit=function (code,id) {
            _this.close();
            keyopt.options.focusElm=retId;
            $('#'+retId).addClass(active||'active');
        };

    };
    var dialogNode, titleNode, contentNode;
    Dialog.prototype = {
        alert: function () {
            this.render('alert');
            this.open(arguments)
        },
        confirm: function () {
            this.render('confirm');
            this.open(arguments)
        },
        open: function (arguments) {
            var arg = arguments;
            var alen = arguments.length;
            //js中预定义了arguments变量，用于表示被传入的变量

            if (alen === 1 && typeof( arg[0] ) === "string") {
                contentNode.innerHTML = arg[0];
                console.log('一个参数，提示内容');
            } else if (alen === 2 && typeof(arg[0]) === "string" && typeof(arg[1]) === "string") {
                titleNode.innerHTML = arg[0];
                contentNode.innerHTML = arg[1];
                console.log('两个参数，标题+提示内容');
            } else if (alen === 2 && typeof(arg[0]) === "string" && typeof(arg[1]) === "function") {
                contentNode.innerHTML = arg[0];
                _global.dialogYesBtn_onsubmit=function (code,id) {
                    arg[1](code,id);
                };
                console.log('两个参数，提示内容+回调');
            } else if (alen === 3 && typeof(arg[0]) === "string" && typeof(arg[1]) === "string" && typeof(arg[2]) === "function") {
                titleNode.innerHTML = arg[0];
                contentNode.innerHTML = arg[1];
                _global.dialogYesBtn_onsubmit=function (code,id) {
                    arg[2](code,id);
                };
                console.log('三个参数，标题+提示内容+回调');
            }
        },
        close: function () {
            dialogNode.style.display='none';
        },
        render:function(type) {
            keyopt.options.focusElm='dialogYesBtn';
            var html = '';
            switch (type) {
                case 'alert':
                    html = '<div class="dialog-title">提示</div>' +
                        '<div class="dialog-content"></div>' +
                        '<div class="dialog-option">' +
                        '<div id="dialogYesBtn" class="dialog-btn dialog-yes-btn active">'+this.o.yes+'</div>' +
                        '</div>';
                    _global.dialogYesBtn_json.go_right='';
                    break;
                case 'confirm':
                    html = '<div class="dialog-title">提示</div>' +
                        '<div class="dialog-content"></div>' +
                        '<div class="dialog-option">' +
                        '<div id="dialogYesBtn" class="dialog-btn dialog-yes-btn active">'+this.o.yes+'</div>' +
                        '<div id="dialogNoBtn" class="dialog-btn dialog-no-btn">'+this.o.no+'</div>' +
                        '</div>';
                    _global.dialogYesBtn_json.go_right='dialogNoBtn';
                    break;
                default:
                    break;
            }
            dialogNode=document.getElementById('dialog');
            if(dialogNode){
                dialogNode.innerHTML=html;
                dialogNode.style.display='block';
            }else {
                var div = document.createElement('div');
                div.id = 'dialog';
                div.className = 'dialog';
                div.innerHTML = html;
                document.body.appendChild(div);
                dialogNode = document.getElementById('dialog');
            }
            titleNode = dialogNode.getElementsByClassName('dialog-title')[0];
            contentNode = dialogNode.getElementsByClassName('dialog-content')[0];
        }
    };

    function importCss() {
        var head = document.getElementsByTagName("head");
        var css = document.createElement("link");
        css.href = '../css/plug-in/dialog.css';
        css.rel = "stylesheet";
        css.type = "text/css";
        head[0].appendChild(css);
    }
    function extend(target, options, deep) {
        for (var key in options) {
            if (options.hasOwnProperty(key)) {
                var copy = options[key];
                if (deep && copy instanceof Array) {
                    target[key] = extend(target[key], copy, deep);
                } else if (deep && copy instanceof Object) {
                    target[key] = extend(target[key], copy, deep);
                } else {
                    target[key] = options[key];
                }
            }
        }
        return target;
    }
    _global = (function () {
        return this || (0, eval)('this');
    }());
    if (typeof module !== "undefined" && module.exports) {
        module.exports = Dialog;
    } else if (typeof define === "function" && define.amd) {
        define(function () {
            return Dialog;
        });
    } else {
        !('Dialog' in _global) && (_global.Dialog = Dialog);
    }
})();